package cho00;

/**
 * Created by 春旺 on 2017/4/15.
 */

public class Complex {
    double r;
    double i;
    //定义构造函数
    public Complex(){
        r=0;
        i=0;
    }
    public Complex(double R,double I){
        R=r;
        I=i;
    }
    //定义setter和getter
    public double setRealPart(double a1){
        r=a1;

        return r;
    }
    public double setImagePart(double a2){
        i=a2;
        return i;
    }
    public String getComplex(){
        return r+i+"i";
    }
    public double getRealPart(){
        return r;
    }
    public double getImagePart(){
        return  i;
    }
    //Override Object
    @Override
    public boolean equals(Object obj){
        if(this == obj){
            return true;
        }
        boolean b = obj instanceof Complex;
        if(b){
            Complex c =(Complex)obj;
            if (this.r==((Complex) obj).r &&this.i==((Complex) obj).i){
                return true;
            }
            else{
                return false ;
            }
        }
        else
            return false;
    }
    public String toString(){
        return "Your Complex is:"+r+i+"i";
    }

    //公共方法
    Object obj;
    Complex u =(Complex)obj;
    double Rr;
    double Ri;
    public void ComplexAdd(Object obj){
        Rr=this.r+((Complex) obj).r;
        Ri=this.i+((Complex) obj).r;
    }
    public void ComplexSub(Object obj){
        Rr=this.r-((Complex) obj).r;
        Ri=this.i-((Complex) obj).r;
    }
    public void ComplexMulti(Object obj){
        Rr=this.r*((Complex) obj).r-this.i*((Complex) obj).i;
        Ri=this.i*((Complex) obj).r+this.r*((Complex) obj).i;
    }
    public void ComplexDiv(Object obj){
        double r2=this.r*((Complex) obj).r+this.i*((Complex) obj).i;
        double i2=this.i*((Complex) obj).r-this.r*((Complex) obj).i;
        Rr=r2/(Math.pow(((Complex) obj).r,2)+Math.pow(((Complex) obj).i,2));
        Ri=i2/(Math.pow(((Complex) obj).r,2)+Math.pow(((Complex) obj).i,2));
    }
    public String getResult(){
        return Rr+Ri+"i";
    }
}


